package com.demo.hxs.service;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.demo.hxs.entity.OrderEntity;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ExcelExportService {
    public Object printExcel(){
        String fileName="C:\\Users\\123\\Desktop\\orders.xlsx";

        // 用于存储读取的数据
        List<OrderEntity> list=new ArrayList<>();

        // 读取excel文件
        EasyExcel.read(fileName, OrderEntity.class, new ReadListener<OrderEntity>() {
            @Override
            public void invoke(OrderEntity orderEntity, AnalysisContext analysisContext) {
                // 每读取一行数据，就调用此方法
                list.add(orderEntity);
            }

            @Override
            public void doAfterAllAnalysed(AnalysisContext analysisContext) {
                // 读取完成后调用此方法
                System.out.println("excel文件读取完成");
            }
        }).sheet().doRead();
        // 将数据导入idea中
        for (OrderEntity orderEntity : list) {
            System.out.println(orderEntity);
        }
        return list;
    }
}
